package com.xiaoma.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoma.dao.UserDao;
import com.xiaoma.service.UserService;
import entity.User;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import utils.JwtUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: xiaoma
 * @Date: 2021/7/9 10:03
 * @description:
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @SneakyThrows
    @Override
    public String login(User user) {
        //查询该用户名是否存在
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        User dbUser = baseMapper.selectOne(wrapper);
        if (dbUser == null) return null;
        //校验密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean flag = encoder.matches(user.getPassword(), dbUser.getPassword());
        if (!flag) return null;
        //生成token返回
        return JwtUtils.getJwtToken(dbUser.getId().toString(), dbUser.getUsername());
    }
}
